#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiTab.au3>
#Region ### START Koda GUI section ### Form=s:\storage service\appsupport\scripts\scm projects\package-express\client\super-user\px package builder\form.kxf
$Form1_1 = GUICreate("PX Package Builder", 770, 628, 192, 115)
;$tabMain = GUICtrlCreateTab(0, 0, 769, 577)
;$tshPackage = GUICtrlCreateTabItem("Package")
$lblName = GUICtrlCreateLabel("Package Name:", 24, 40, 81, 17)
$ibxName = GUICtrlCreateInput("My-New-Package", 112, 40, 257, 21)
$grpProcedure = GUICtrlCreateGroup("Procedure", 8, 64, 737, 497)
$cbxInstall = GUICtrlCreateCheckbox("Application Install", 32, 96, 113, 17)
$ibxFile = GUICtrlCreateInput("", 144, 96, 505, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$btnBrowseFile = GUICtrlCreateButton("Browse", 656, 96, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$lblInstallParams = GUICtrlCreateLabel("Installation Parameters:", 144, 128, 113, 17)
$ibxParam = GUICtrlCreateInput("/silent", 256, 128, 121, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$grpUninstall = GUICtrlCreateGroup("Uninstall Procedure", 144, 160, 585, 233)
$rdoUnCommand = GUICtrlCreateRadio("Command", 152, 184, 73, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$ibxUnCommand = GUICtrlCreateInput("wmic product where name like %Your-Application% call uninstall", 224, 184, 425, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$rdoScript = GUICtrlCreateRadio("Script", 152, 216, 57, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$edScript = GUICtrlCreateEdit("", 224, 216, 425, 121)
GUICtrlSetData(-1, "")
GUICtrlSetState(-1, $GUI_DISABLE)
$lblStartWith = GUICtrlCreateLabel("Run With:", 224, 344, 54, 17)
$ibxStartWith = GUICtrlCreateInput("C:\Windows\System32\cmd.exe", 280, 344, 241, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$lblExtension = GUICtrlCreateLabel("Extension:", 528, 344, 53, 17)
$ibxExtension = GUICtrlCreateInput("bat", 584, 344, 65, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$cbxFolder = GUICtrlCreateCheckbox("Folder Install:", 32, 416, 89, 17)
$ibxFolder = GUICtrlCreateInput("C:\Some\Folder", 152, 416, 497, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$btnBrowseFolder = GUICtrlCreateButton("Browse", 656, 416, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
$lblDest = GUICtrlCreateLabel("Destination:", 152, 448, 60, 17)
$ibxDest = GUICtrlCreateInput("%UserProfile%\Desktop", 216, 448, 433, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$lblVersion = GUICtrlCreateLabel("Version:", 384, 40, 42, 17)
$ibxVersion = GUICtrlCreateInput("1", 432, 40, 121, 21)
;$tshConfiguration = GUICtrlCreateTabItem("Configuration")
;GUICtrlCreateEdit("", 16, 32, 737, 529, BitOR($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
;GUICtrlSetData(-1, "")
;GUICtrlCreateTabItem("")
$btnBuild = GUICtrlCreateButton("Build", 360, 592, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $cbxInstall
			setInstallArea()
		Case $rdoUnCommand
			setUninstallArea()
		Case $rdoScript
			setUninstallArea()
		Case $cbxFolder
			setFolderArea()
		Case $btnBuild
			$inApplic = GUICtrlRead($cbxInstall)
			$inFolder = GUICtrlRead($cbxFolder)
			If GUICtrlRead($cbxInstall) == 1 Then
				$installFile = True
			Else
				$installFile = False
			EndIf
			If GUICtrlRead($cbxFolder) == 1 Then
				$installFolder = True
			Else
				$installFolder = False
			EndIf
			If GUICtrlRead($rdoUnCommand) == 1 Then
				$commandUninstall = True
			Else
				$commandUninstall = False
			EndIf
			If GUICtrlRead($rdoScript) == 1 Then
				$scriptUninstall = True
			Else
				$scriptUninstall = False
			EndIf
			$packageName = GUICtrlRead($ibxName)
			$packageVersion = GUICtrlRead($ibxVersion)
			$installFilePath = GUICtrlRead($ibxFile)
			$installParam = GUICtrlRead($ibxParam)
			$folder = GUICtrlRead($ibxFolder)
			$folderDest = GUICtrlRead($ibxDest)
			$uninCommand = GUICtrlRead($ibxUnCommand)
			$uninScript = GUICtrlRead($edScript)
			$scriptExec = GUICtrlRead($ibxStartWith)
			$scriptExtension = GUICtrlRead($ibxExtension)
			$xml = "<package>" & @CRLF
			$xml = $xml & "<name>" & $packageName & "</name>" & @CRLF
			$xml = $xml & "<version>" & $packageVersion & "</version>" & @CRLF
			$xml = $xml & "<install>" & @CRLF
			If $installFile == True Then
				$xml = $xml & "<file>" & @CRLF
				$xml = $xml & "<filename>" & $installFilePath & "</filename>" & @CRLF
				$xml = $xml & "<parameter>" & $installParam & "</parameter>" & @CRLF
				$xml = $xml & "</file>" & @CRLF
			EndIf
			If $installFolder == True Then
				$xml = $xml & "<folder>" & @CRLF
				$xml = $xml & "<foldername>" & $folder & "</foldername>" & @CRLF
				$xml = $xml & "<folderdest>" & $folderDest & "</folderdest>" & @CRLF
				$xml = $xml & "</folder>" & @CRLF
			EndIf
			$xml = $xml & "</install>" & @CRLF
			$xml = $xml & "<uninstall>" & @CRLF
			If $commandUninstall == True Then
				$xml = $xml & "<command>" & $uninCommand & "</command>" & @CRLF
			EndIf
			If $scriptUninstall == True Then
				$xml = $xml & "<script>" & @CRLF
				$xml = $xml & "<exec>" & $scriptExec & "</exec>" & @CRLF
				$xml = $xml & "<content>" & $uninScript & "</content>" & @CRLF
				$xml = $xml & "</script>" & @CRLF
			EndIf
			$xml = $xml & "</uninstall>" & @CRLF
			$xml = $xml & "</package>"
			Local $file = FileOpen(@ScriptDir & "\label.xml", 1)
			FileWrite($file, $xml)
			FileClose($file)

			$exec = "python " & @ScriptDir & "\Create-Package.py -x label.xml -n " & $packageName & " -f " & $installFilePath

			Local $file = FileOpen(@ScriptDir & "\build.bat", 1)
			FileWrite($file, $exec)
			FileClose($file)

			RunWait(@ScriptDir & "\build.bat")

			If FileExists(@ScriptDir & "\Create-Package.py") Then
				If $installFile == True Then
					RunWait("python " & @ScriptDir & "\Create-Package.py -x label.xml -n " & $packageName & " -f " & $installFilePath)
					;RunWait(@ScriptDir & "\Create-Package.py -x label.xml -n " & $packageName & " -f " & $installFilePath)
					;FileDelete(@ScriptDir & "\label.xml")
				EndIf
			EndIf
			Exit
		Case $btnBrowseFile
			$installFilePath_ = FileOpenDialog("Select File", @ScriptDir, "Installer (*.exe;*.msi)", 1 + 4)
			$ibxFile = GUICtrlCreateInput($installFilePath_, 144, 96, 505, 21)
			$fileArray = StringSplit($installFilePath_,".")
			For $i = 0 to $fileArray[0]
				$ext = $fileArray[$i]
			Next
			$fileArray = StringSplit($installFilePath_,"\")
			For $i = 0 to $fileArray[0]
				$filename = StringReplace($fileArray[$i],"." & $ext,"")
			Next
			$ibxUnCommand = GUICtrlCreateInput("wmic product where name like %" & $filename & "% call uninstall", 224, 184, 425, 21)
			If $ext == "msi" Then
				$ibxParam = GUICtrlCreateInput("/quiet /norestart", 256, 128, 121, 21)
			EndIf
		Case $btnBrowseFolder
			$installFolderPath_ = FileSelectFolder("Choose a folder.", "")
			$ibxFolder = GUICtrlCreateInput($installFolderPath_, 152, 416, 497, 21)
		Case $GUI_EVENT_CLOSE
			Exit

	EndSwitch
WEnd

Func setInstallArea()
	If GUICtrlRead($cbxInstall) == 1 Then
		GUICtrlSetState($ibxFile, $GUI_ENABLE)
		GUICtrlSetState($btnBrowseFile, $GUI_ENABLE)
		GUICtrlSetState($ibxParam, $GUI_ENABLE)
		GUICtrlSetState($rdoUnCommand, $GUI_ENABLE)
		GUICtrlSetState($rdoScript, $GUI_ENABLE)
	Else
		GUICtrlSetState($ibxFile, $GUI_DISABLE)
		GUICtrlSetState($btnBrowseFile, $GUI_DISABLE)
		GUICtrlSetState($ibxParam, $GUI_DISABLE)
		GUICtrlSetState($rdoUnCommand, $GUI_DISABLE)
		GUICtrlSetState($rdoScript, $GUI_DISABLE)
		GUICtrlSetState($edScript, $GUI_DISABLE)
		GUICtrlSetState($ibxStartWith, $GUI_DISABLE)
		GUICtrlSetState($ibxExtension, $GUI_DISABLE)
		GUICtrlSetState($ibxUnCommand, $GUI_DISABLE)
	EndIf
EndFunc

Func setUninstallArea()
	If GUICtrlRead($rdoScript) == 4 Then
		GUICtrlSetState($edScript, $GUI_DISABLE)
		GUICtrlSetState($ibxStartWith, $GUI_DISABLE)
		GUICtrlSetState($ibxExtension, $GUI_DISABLE)
		GUICtrlSetState($ibxUnCommand, $GUI_ENABLE)
	Else
		GUICtrlSetState($edScript, $GUI_ENABLE)
		GUICtrlSetState($ibxStartWith, $GUI_ENABLE)
		GUICtrlSetState($ibxExtension, $GUI_ENABLE)
		GUICtrlSetState($ibxUnCommand, $GUI_DISABLE)
	EndIf
EndFunc

Func setFolderArea()
	If GUICtrlRead($cbxFolder) == 1 Then
		GUICtrlSetState($ibxFolder, $GUI_ENABLE)
		GUICtrlSetState($ibxDest, $GUI_ENABLE)
		GUICtrlSetState($btnBrowseFolder, $GUI_ENABLE)
		GUICtrlSetState($rdoScript, $GUI_ENABLE)
	Else
		GUICtrlSetState($ibxFolder, $GUI_DISABLE)
		GUICtrlSetState($ibxDest, $GUI_DISABLE)
		GUICtrlSetState($btnBrowseFolder, $GUI_DISABLE)
		GUICtrlSetState($rdoScript, $GUI_DISABLE)
	EndIf
EndFunc